<script setup lang="ts">
import { computed, ref, watch } from 'vue'

const index = ref(0)
const sliderEl = ref<HTMLDivElement | null>(null)

watch(index, (newV) => {
  if (sliderEl && sliderEl.value) {
    sliderEl.value.style.setProperty('--l', newV + '')
  }
})

const strs = ['first', 'second', 'third']
let leftActive = computed(() => {
  return strs[index.value]
})
</script>

<template>
  <div class="slider-radio-button">
    <div ref="sliderEl" class="container">
      <div class="slider">
        <div class="red"></div>
        <div class="blue"></div>
        <div class="green"></div>
      </div>
      <div class="points">
        <div class="point" v-for="i in 3" @click="index = i - 1" :key="i"></div>
        <div :class="['dot', leftActive]"></div>
      </div>
    </div>
  </div>
</template>
<style lang="scss" scoped>
.slider-radio-button {
  width: 100%;
  height: 100%;

  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #3e4c5a;

  .container {
    width: 58%;
    height: 55%;
    position: relative;
    overflow: hidden;

    .slider {
      width: 100%;
      height: 100%;
      display: flex;
      flex-wrap: nowrap;
      transform: translateX(calc(var(--l) * -100%));
      transition: all 0.6s ease-in-out;

      div {
        width: 100%;
        height: 100%;
        flex-shrink: 0;
      }

      .red {
        background-color: #9b59b6;
      }

      .blue {
        background-color: #3498db;
      }

      .green {
        background-color: #1abc9c;
      }
    }

    .points {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 200px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      .point {
        width: 50px;
        height: 50px;
        border-radius: 100%;
        border: 2px solid #fff;
      }

      .dot {
        background-color: #fff;
        width: 45px;
        height: 45px;
        border-radius: 100%;
        position: absolute;
        transition: all 0.6s ease-in-out;

        &.first {
          left: 11px;
        }

        &.second {
          left: 77.5px;
        }

        &.third {
          left: 144.2px;
        }
      }
    }
  }
}
</style>
